package Queue;

/**
 * User:辰柒
 * Date:2022-10-03
 * Time:15:47
 */

import java.util.LinkedList;
import java.util.Queue;

/**
 * 循环队列
 */
class MyCircularQueue {
    private int front;
    private int rear;
    private int capacity;
    private int[] elements;//通过计数器的方式实现

    public MyCircularQueue(int k) {
        capacity = k + 1;
        elements = new int[capacity];
        rear = front = 0;
    }

    public boolean enQueue(int value) {//插入一个元素
        if (isFull()) {
            return false;
        }
        elements[rear] = value;
        rear = (rear + 1) % capacity;
        return true;
    }

    public boolean deQueue() {//删除一个元素
        if (isEmpty()) {
            return false;
        }
        front = (front + 1) % capacity;
        return true;
    }


    public int Front() {//获取队首元素
        if (isEmpty()) {
            return -1;
        }
        return elements[front];
    }
    public int Rear() {//获取队尾元素
        if (isEmpty()) {
            return -1;
        }
        return elements[(rear - 1 + capacity) % capacity];
    }

    public boolean isEmpty() {
        return rear == front;
    }

    public boolean isFull() {
        return ((rear + 1) % capacity) == front;
    }

    public static void main(String[] args) {

    }
}

